/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.application.isradeleon.notify.helper;

import com.application.isradeleon.notify.utils.HttpUtils;
import ohos.agp.render.Canvas;
import ohos.agp.render.Texture;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.PixelMapShader;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * PixelMap操作
 */
public class PixelMapHelper {
    /**
     * 从url中获取PixelMap
     *
     * @param url url
     * @return pixelMap
     */
    public static PixelMap getPixelMapFromUrl(String url) {
        PixelMap pm = null;
        InputStream stream = HttpUtils.getStream(url);
        if (stream != null) {
            try {
                ImageSource source = ImageSource.create(stream, new ImageSource.SourceOptions());
                ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
                options.desiredPixelFormat = PixelFormat.ARGB_8888;
                pm = source.createPixelmap(options);
            } finally {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return pm;
    }

    /**
     * 从资源中获取PixelMap
     *
     * @param context 上下文
     * @param res     资源
     * @return pixelMap
     */
    public static PixelMap getPixelMapFromRes(Context context, int res) {
        PixelMap pm = null;
        InputStream stream = null;
        try {
            stream = context.getResourceManager().getResource(res);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            ImageSource imageSource = ImageSource.create(stream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            pm = imageSource.createPixelmap(decodingOptions);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return pm;
    }

    /**
     * 将pixelMap转成圆角
     *
     * @param pixelMap 源pixelMap
     * @return 圆角pixelMap
     */
    public static PixelMap toCircleBitmap(PixelMap pixelMap) {
        Size originSize = pixelMap.getImageInfo().size;
        PixelMap.InitializationOptions options =
                new PixelMap.InitializationOptions();
        options.size = new Size(originSize.width, originSize.height);
        options.pixelFormat = PixelFormat.ARGB_8888;
        options.editable = true;
        PixelMap circlePixelMap = PixelMap.create(options);
        Canvas canvas = new Canvas();
        Texture texture = new Texture(circlePixelMap);
        canvas.setTexture(texture);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        PixelMapHolder pixelMapHolder =
                new PixelMapHolder(PixelMap.create(pixelMap, options));
        PixelMapShader shader = new PixelMapShader(pixelMapHolder,
                Shader.TileMode.CLAMP_TILEMODE,
                Shader.TileMode.CLAMP_TILEMODE);
        paint.setShader(shader, Paint.ShaderType.PIXELMAP_SHADER);
        canvas.drawCircle(
                originSize.width * 1.0f / 2,
                originSize.height * 1.0f / 2,
                originSize.width * 1.0f / 2, paint);
        return circlePixelMap;
    }
}